package com.ebs.md.fxmonitor.client.impl;

	import java.util.ArrayList;
	import java.util.HashMap;
	import java.util.List;
	import java.util.Map;

	public class ArbMsgSP 
	{
		private Map<Integer,String> m_msgToSPMap;	
		private Map<String,List<Integer>> m_spToMsgTypeMap;
		
		
		
		public void setMsgToMap(Map<Integer,String> msgToSPMap)
		{
			m_msgToSPMap = msgToSPMap;
			m_spToMsgTypeMap = populateSpToMsgType(null, true);
		}
		
		
		public Map<String, String> getTypeToSp(List<Integer> subscribeTo)
		{
			Map<String,String> toReturn = new HashMap<String, String>();
			if((subscribeTo != null) && (subscribeTo.size() > 0) && (m_msgToSPMap != null))
			{
				for(Integer nextType : subscribeTo)
				{
					if(m_msgToSPMap.containsKey(nextType))
					{
						toReturn.put("" + nextType, m_msgToSPMap.get(nextType));
					}
				}
			}			
			return toReturn;
		}
		
		
		public Map<String,List<Integer>> getSpPerSubscription(List<Integer> subscribeTo)
		{
			Map<String,List<Integer>> toReturn;
			if((subscribeTo == null)||(subscribeTo.size() <= 0))
			{
				toReturn = m_spToMsgTypeMap;
			}
			else
			{
				toReturn = populateSpToMsgType(subscribeTo, false);
			}
			return toReturn;
		}
		
		private Map<String,List<Integer>> populateSpToMsgType(List<Integer> msgTypes, 
				boolean initFlag)
		{
			Map<String,List<Integer>> spToMsgTypeMap = new HashMap<String,List<Integer>>();
			if((msgTypes != null) && (msgTypes.size() > 0))
			{
				for(Integer nextMsgType : msgTypes)
				{
					addMsgType(nextMsgType, spToMsgTypeMap);
				}
			}
			else if(initFlag)
			{
				for(Integer nextMsgType : m_msgToSPMap.keySet())
				{
					addMsgType(nextMsgType, spToMsgTypeMap);
				}	
			}
			else
			{
				spToMsgTypeMap = m_spToMsgTypeMap;
			}
			return spToMsgTypeMap;
		}
		
		private void addMsgType(int nextMsgType, Map<String,List<Integer>> spToMsgTypeMap)
		{
			String spName = m_msgToSPMap.get(nextMsgType);
			if(spName != null)
			{
				List<Integer> spMsgTypeList = spToMsgTypeMap.get(spName);
				if(spMsgTypeList == null)
				{
					spMsgTypeList = new ArrayList<Integer>();
					spToMsgTypeMap.put(spName, spMsgTypeList);
				}
				if(!spMsgTypeList.contains(nextMsgType))
				{
					spMsgTypeList.add(nextMsgType);
				}
			}		
		}

	}


